Explanations

Access Control

Default
  • Class members (properties, methods, etc.):

    • Default: private

    • If you do not specify an access modifier, the member is private and accessible only within the class itself.

  • Top-level classes and structs:

    • Default: internal

    • If you do not specify a modifier, the class or struct is accessible only within the same assembly  (usually the same project in .NET).

Modifiers
abstract
static
  • "Static variables are used to pass information across different instances of the same class, not across different classes".

Classes

using System;

class Person
{
    // Properties
    public string Name { get; set; }
    public int Age { get; set; }

    // Constructor
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    // Method to display information
    public void DisplayInfo()
    {
        Console.WriteLine($"Name: {Name}, Age: {Age}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Creating an instance of the Person class
        Person person = new Person("John", 25);
        person.DisplayInfo();
    }
}

Set / Get

  • Tutorial and examples of simplified Set/Get usage: ChristianHur video

  • Without access control for Set and Get:

public String car;
  • Simplified:

    public String car {get; private set;}
    
    • Using { set; get; }  creates an automatic property . This means the compiler automatically generates a private field to store the value, without needing to explicitly declare it in the class.

    • Equivalent to doing:

      private String _car; // Manually generated private field
      
      public String car
      {
          get { return _car; }
          set { _car = value; }
      }
      
  • Normal:

    public String car {
        get {
            return car;
        }
        private set {
            car = value;
        }
    }
    

Delegates (Callables in GDScript)

// Stores the function, making the variable a delegate.
variable = function;

// Stores the function's return value.
variable = function();

Generics

  • "Work with multiple Types".

void function<T>(T a, T b) {
}

function<float>(a, b);

Import and Export

Godot
  • .

Unity
  • [SerializeField]

    • In Unity, exposes the property in the Inspector.

Events

Publisher
  • Uses Invoke.

Subscriber
  • 'subscribe' is done via 'myEvent += created_function'.

  • 'unsubscribe' is done via 'myEvent -= created_function'.

Creating events
  • Publisher:

using System;

public class MyScript_A : MonoBehaviour {

    // Creates the event, using 'arguments' like the event's 'EventArgs'.
    public event EventHandler<Arguments> OnPressSpace;

    // Function used as the 'Generic Parameter' for 'OnPressSpace'.
    public class Arguments : EventArgs {
        public int value;
    }
    
    private void Update() {
        if (Input.GetKeyDown(KeyCode.Space)) {
            // Fires the event with its argument.
            OnPressSpace?.Invoke(this, new Arguments {value = 2});
        }
    }
    
}
  • Subscriber:

using System;

public class MyScript_B : MonoBehaviour {

    private Start() {
        // Sets the variable 'myScript_A' to type 'MyScript_A' and stores the 'MyScript_A' component.
        MyScript_A myScript_A = GetComponent<MyScript_A>();

        // Subscribes: Accesses 'OnPressSpace' event from 'myScript_A' and "adds" the created function 'Event_OnPressSpace'.
        myScript_A.OnPressSpace += Event_OnPressSpace;
    }

    // Uses the created function 'Event_OnPressSpace' to receive the 'object' and 'arguments' passed during event firing.
    private void Event_OnPressSpace(object sender, myScript_A.Arguments e) {
        Debug.log("Value: " + e.value);
    }
    
}